/**
 * 获取当前页面的URL（针对IOS进行截取特殊处理:tongtong-https://）
 */
export const getCurrentPageUrl = () => {
    const url = window.location.href.match(/https?:\/\/([\w.]+\/?)\S*/)[0].replace("isVue=true", "");
    return url[url.length - 1] === "?" ? url.replace("?", "") : url;
};

/**
 * 页面返回方法
 */
export const pageReturn = () => {
    const url = new URL(window.location.href);
    if (url.searchParams.get("isVue") === "true") {
        window.history.back();
    } else if (window.TongBridge) {
        window.TongBridge.closeWebview ? window.TongBridge.closeWebview() : window.history.back();
    }
};
/**
 * vue路由跳转path地址转换
 */
export const routerPath = path => {
    if (path) {
        path += (path.indexOf("?") > -1 ? "&" : "?") + "isVue=true";
    } else {
        path = {
            isVue: true
        };
    }
    return path;
};
export function getDeviceType() {
    const userAgent = navigator.userAgent || navigator.vendor || window.opera;

    if (/android/i.test(userAgent)) {
        return "android";
    }

    if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        return "IOS";
    }

    return "Unknown";
}

/**
 * 空闲位自动补全0
 * @param num 数据
 * @param len 设定长度
 */
export const autoFillZero = (num, len) => {
    return (Array(len).join("0") + num).slice(-len);
};
/**
 * 将时间戳格式化为日期时间字符串
 * @param timestamp - 时间戳(毫秒)
 * @param dPar - 日期分隔符,默认为"-"
 * @param par - 日期与时间之间的分隔符,默认为" "
 * @param tPar - 时间分隔符,默认为":"
 * @returns 格式化后的日期时间字符串,如"2023-12-25 12:30:45"
 */
export const formatTime = (timestamp, dPar = "-", par = " ", tPar = ":") => {
    const mydate = new Date(Number(timestamp));
    const year = mydate.getFullYear();
    const month = autoFillZero(mydate.getMonth() + 1, 2);
    const date = autoFillZero(mydate.getDate(), 2);
    const hours = autoFillZero(mydate.getHours(), 2);
    const minutes = autoFillZero(mydate.getMinutes(), 2);
    const seconds = autoFillZero(mydate.getSeconds(), 2);

    return year + dPar + month + dPar + date + par + hours + tPar + minutes + tPar + seconds;
};
export const formatterDate = (type, option) => {
    if (type === "year") {
        option.text += "年";
    }
    if (type === "month") {
        option.text += "月";
    }
    if (type === "day") {
        option.text += "日";
    }
    return option;
};
export function getAgent() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf("android") != -1) {
        // 这是Android设备
        return "android";
    } else if (!!ua.match(/\(i[^;]+;( U;)? cpu.+mac os x/)) {
        // 这是iOS设备
        return "ios";
    } else {
        // 未知设备类型
        return "else";
    }
}
export function hiddenPay() {
    return getAgent() === "ios";
}
